home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows 95 API Bible
/
Windows 95 API Bible 3 Disc Set.iso
/
Win32 API Bible Book 2 of 3
/
CHAPTER6
/
ARRANGE.C
next >
Wrap
C/C++ Source or Header
|
1996-03-06
|
16KB
|
458 lines
#include <windows.h>
#include <commctrl.h>
#include "arrange.h"
#if defined (WIN32)
#define IS_WIN32 TRUE
#else
#define IS_WIN32 FALSE
#endif
#define IS_NT IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
#define IS_WIN32S IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
#define IS_WIN95 (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
HINSTANCE hInst; // current instance
LPCTSTR lpszAppName = "MyApp";
LPCTSTR lpszTitle = "ListView_Arrange()";
BOOL RegisterWin95( CONST WNDCLASS* lpwc );
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HWND hWnd;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (hInstance, lpszAppName);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = lpszAppName;
wc.lpszClassName = lpszAppName;
if ( IS_WIN95 )
{
if ( !RegisterWin95( &wc ) )
return( FALSE );
}
else if ( !RegisterClass( &wc ) )
return( FALSE );
hInst = hInstance;
hWnd = CreateWindow( lpszAppName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,
NULL,
NULL,
hInstance,
NULL
);
if ( !hWnd )
return( FALSE );
ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );
while( GetMessage( &msg, NULL, 0, 0) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return( msg.wParam );
}
BOOL RegisterWin95( CONST WNDCLASS* lpwc )
{
WNDCLASSEX wcex;
wcex.style = lpwc->style;
wcex.lpfnWndProc = lpwc->lpfnWndProc;
wcex.cbClsExtra = lpwc->cbClsExtra;
wcex.cbWndExtra = lpwc->cbWndExtra;
wcex.hInstance = lpwc->hInstance;
wcex.hIcon = lpwc->hIcon;
wcex.hCursor = lpwc->hCursor;
wcex.hbrBackground = lpwc->hbrBackground;
wcex.lpszMenuName = lpwc->lpszMenuName;
wcex.lpszClassName = lpwc->lpszClassName;
// Added elements for Windows 95.
//...............................
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName,
IMAGE_ICON, 16, 16,
LR_DEFAULTCOLOR );
return RegisterClassEx( &wcex );
}
LPCTSTR lpszData[4] = { "Circle", "Rectangle", "Cross", "Check" };
HIMAGELIST CreateMultiDrag( HWND hList, int nItem, POINT* pHotPoint );
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
static POINT ptHotSpot, pt;
static int nDragItem = -1;
static HWND hList = NULL;
static HIMAGELIST hCursors = NULL;
static HIMAGELIST hDragList = NULL;
switch( uMsg )
{
case WM_CREATE :
{
HIMAGELIST hImage;
ICONINFO info;
InitCommonControls();
hCursors = ImageList_Create( 32, 32, ILC_COLOR4 | ILC_MASK, 1, 1 );
hImage = ImageList_Create( 32, 32, ILC_COLOR4 | ILC_MASK, 4, 1 );
hList = CreateWindowEx( 0, WC_LISTVIEW, "",
WS_CHILD | WS_VISIBLE | LVS_ICON,
10, 100, 100, 100,
hWnd,
(HMENU)1,
hInst,
NULL);
if ( hList )
{
LV_ITEM item;
HBITMAP hBmp;
int i;
ListView_SetImageList( hList, hImage, LVSIL_NORMAL );
item.mask = LVIF_TEXT | LVIF_IMAGE;
for( i=0; i<4; i++ )
{
hBmp = LoadBitmap( hInst, lpszData[i] );
item.pszText = (LPTSTR)lpszData[i];
item.iItem = i;
item.iSubItem = 0;
item.iImage = ImageList_AddMasked( hImage, hBmp, RGB( 0, 255, 0 ) );
ListView_InsertItem( hList, &item );
DeleteObject( hBmp );
}
}
ImageList_AddIcon( hCursors, LoadCursor( NULL, IDC_ARROW ) );
// Retrieve the hot spot information from the cursor.
//...................................................
GetIconInfo( LoadCursor( NULL, IDC_ARROW ), &info );
ptHotSpot.x = info.xHotspot;
ptHotSpot.y = info.yHotspot;
// Clean up the bitmaps from GetIconInfo().
//.........................................
DeleteObject( info.hbmMask );
DeleteObject( info.hbmColor );
}
break;
case WM_SIZE :
if ( hList )
MoveWindow( hList, 0, 0, LOWORD( lParam ), HIWORD( lParam ), FALSE );
if ( wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED )
ListView_RedrawItems( hList, 0, ListView_GetItemCount( hList ) );
break;
case WM_NOTIFY :
if ( wParam == 1 )
{
NM_LISTVIEW* pNM = (NM_LISTVIEW*)lParam;
switch ( pNM->hdr.code )
{
case LVN_BEGINDRAG :
// Create the drag image.
//.......................
if ( ListView_GetSelectedCount( hList ) == 1 )
hDragList = ListView_CreateDragImage( hList, pNM->iItem, &pt );
else
hDragList = CreateMultiDrag( hList, pNM->iItem, &pt );
// Start dragging and set the drag cursor.
//........................................
ImageList_BeginDrag( hDragList, 0, pNM->ptAction.x-pt.x,
pNM->ptAction.y-pt.y );
ImageList_SetDragCursorImage( hCursors, 0, pNM->ptAction.x-pt.x-ptHotSpot.x,
pNM->ptAction.y-pt.y-ptHotSpot.y );
ImageList_DragEnter( hList, pNM->ptAction.x, pNM->ptAction.y );
// Capture the mouse and hide the normal cursor.
//..............................................
SetCapture( hWnd );
ShowCursor( FALSE );
pt = pNM->ptAction;
nDragItem = pNM->iItem;
break;
}
}
break;
case WM_MOUSEMOVE :
// Move the drag cursor if dragging.
//..................................
if ( hDragList )
{
ImageList_DragMove( LOWORD( lParam ), HIWORD( lParam ) );
}
break;
case WM_LBUTTONUP :
// If dragging, drop the object in its new place.
//...............................................
if ( hDragList )
{
POINT ptPos;
ImageList_DragLeave( hList );
ImageList_EndDrag();
ImageList_Destroy( hDragList );
hDragList = NULL;
ReleaseCapture();
ShowCursor( TRUE );
if ( ListView_GetSelectedCount( hList ) == 1 )
{
ListView_GetItemPosition( hList, nDragItem, &ptPos );
ListView_SetItemPosition( hList, nDragItem, ptPos.x+(LOWORD( lParam )-pt.x), ptPos.y+(HIWORD( lParam )-pt.y) );
}
else // Dragging multiple items.
{
int nCur = -1;
while ( (nCur = ListView_GetNextItem( hList, nCur, LVNI_SELECTED )) > -1 )
{
ListView_GetItemPosition( hList, nCur, &ptPos );
ListView_SetItemPosition( hList, nCur, ptPos.x+(LOWORD( lParam )-pt.x), ptPos.y+(HIWORD( lParam )-pt.y) );
}
}
}
break;
case WM_COMMAND :
switch( LOWORD( wParam ) )
{
case IDM_ARRANGE :
ListView_Arrange( hList, LVA_DEFAULT );
break;
case IDM_ABOUT :
DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
break;
case IDM_EXIT :
DestroyWindow( hWnd );
break;
}
break;
case WM_DESTROY :
if ( ListView_GetImageList( hList, LVSIL_NORMAL ) )
ImageList_Destroy( ListView_GetImageList( hList, LVSIL_NORMAL ) );
PostQuitMessage(0);
break;
default :
return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
}
return( 0L );
}
LRESULT CALLBACK About( HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return (TRUE);
case WM_COMMAND:
if ( LOWORD(wParam) == IDOK
|| LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, TRUE);
return (TRUE);
}
break;
}
return (FALSE);
}
HIMAGELIST CreateMultiDrag( HWND hList, int nItem, POINT* pHotPoint )
{
HBITMAP hBmp1, hBmp2, hTmp1, hTmp2, hImage1, hImage2;
HIMAGELIST hImageList;
HPEN hPen;
RECT rect, rect1, rect2, rect3, rect4;
HDC hMemDC1, hMemDC2;
HDC hDC = GetDC( hList );
int i = 0;
int nCur = -1;
int nSel = ListView_GetSelectedCount( hList );
// Find out the size of the rect that
// contains all items selected.
//...................................
for ( i=0; i<nSel; i++ )
{
nCur = ListView_GetNextItem( hList, nCur, LVNI_SELECTED );
ListView_GetItemRect( hList, nCur, &rect1, LVIR_LABEL );
ListView_GetItemRect( hList, nCur, &rect2, LVIR_ICON );
UnionRect( &rect4, &rect1, &rect2 );
if( i == 0 )
rect3 = rect4;
UnionRect( &rect, &rect3, &rect4 );
rect3 = rect;
}
pHotPoint->x = rect.left;
pHotPoint->y = rect.top;
// Create 2 compatible bitmaps and device contexts,
// one for the drag image and one for the drag mask.
//..................................................
hBmp1 = CreateCompatibleBitmap( hDC, rect.right - rect.left, rect.bottom - rect.top );
hBmp2 = CreateCompatibleBitmap( hDC, rect.right - rect.left, rect.bottom - rect.top );
hPen = CreatePen( PS_SOLID, 1, RGB( 128, 128, 128 ) );
hMemDC1 = CreateCompatibleDC( hDC );
hMemDC2 = CreateCompatibleDC( hDC );
// Select the empty bitmaps, brushes, and pens
// into the compatible device contexts.
//............................................
SelectObject( hMemDC1, hBmp1 );
SelectObject( hMemDC2, hBmp2 );
SelectObject( hMemDC1, GetStockObject( BLACK_BRUSH ) );
SelectObject( hMemDC2, GetStockObject( WHITE_BRUSH ) );
SelectObject( hMemDC1, hPen );
SelectObject( hMemDC2, GetStockObject( BLACK_PEN ) );
rect3.left -= rect.left;
rect3.right -= rect.left;
rect3.top -= rect.top;
rect3.bottom -= rect.top;
// Fill the entire bitmap with the
// appropriate color.
//................................
FillRect( hMemDC1, &rect3, (HBRUSH)GetStockObject( BLACK_BRUSH ) );
FillRect( hMemDC2, &rect3, (HBRUSH)GetStockObject( WHITE_BRUSH ) );
// Draw the images on the bitmaps.
//................................
nCur = -1;
for( i = 0; i < nSel; i++ )
{
nCur = ListView_GetNextItem( hList, nCur, LVNI_SELECTED );
ListView_GetItemRect( hList, nCur, &rect2, LVIR_LABEL );
ListView_GetItemRect( hList, nCur, &rect3, LVIR_ICON );
// Paint the Image.
//.................
Rectangle( hMemDC1, rect3.left - rect.left, rect3.top - rect.top,
rect3.right - rect.left, rect3.bottom - rect.top );
MoveToEx( hMemDC1, rect2.left - rect.left,
(rect2.top - rect.top)+
((rect2.bottom - rect.top) - (rect2.top - rect.top))/2, NULL );
LineTo( hMemDC1, rect2.right - rect.left,
(rect2.top - rect.top)+
((rect2.bottom - rect.top) - (rect2.top - rect.top))/2 );
// Paint the Mask.
//................
Rectangle( hMemDC2, rect3.left - rect.left, rect3.top - rect.top,
rect3.right - rect.left, rect3.bottom - rect.top );
MoveToEx( hMemDC2, rect2.left - rect.left,
(rect2.top - rect.top)+
((rect2.bottom - rect.top) - (rect2.top - rect.top))/2, NULL );
LineTo( hMemDC2, rect2.right - rect.left,
(rect2.top - rect.top)+
((rect2.bottom - rect.top) - (rect2.top - rect.top))/2 );
}
// Create temporary bitmaps and select them
// into the device context which will then return
// a handle to the bitmaps that we just made.
//...............................................
hTmp1 = CreateCompatibleBitmap( hDC, 1, 1 );
hTmp2 = CreateCompatibleBitmap( hDC, 1, 1 );
hImage1 = (HBITMAP)SelectObject( hMemDC1, hTmp1 );
hImage2 = (HBITMAP)SelectObject( hMemDC2, hTmp2 );
// Create the Drag image list and add the images.
//...............................................
hImageList = ImageList_Create( rect.right-rect.left, rect.bottom-rect.top, TRUE, 1, 1 );
ImageList_Add( hImageList, hImage1, hImage2 );
ReleaseDC( hList, hDC );
DeleteDC( hMemDC1 );
DeleteDC( hMemDC2 );
DeleteObject( hBmp1 );
DeleteObject( hBmp2 );
DeleteObject( hTmp1 );
DeleteObject( hTmp2 );
DeleteObject( hPen );
return( hImageList );
}